programming4us
           
 
 
SQL Server

Using XML in SQL Server 2008: Relational Data As XML - The FOR XML Modes (part 2) - Working with Binary Columns

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
1/6/2011 3:14:20 PM
Working with Binary Columns

Even though XML is purely a text-based markup language, FOR XML still has the capability to generate XML that contains data selected from binary–data-typed columns, such as image, binary, and varbinary. To do this, SQL Server base-64 encodes the data, resulting in a long character string.

To implement this in a query, you add joins from the table Production.Product to Production.ProductProductPhoto and then to Production.ProductPhoto, which contains the varbinary(max)–data-typed ThumbNailPhoto column. Then you add the keywords BINARY BASE64 to the FOR XML clause. Listing 5 illustrates this and also shows the schema generated by the XMLSCHEMA keyword. (Note that the base-64 character data is truncated in the listing for brevity with the character string {...}.)

Listing 5. A SELECT Statement That Uses FOR XML RAW and the BINARY BASE64 Option
SELECT TOP 1 Name, ListPrice, Color, Weight, ThumbNailPhoto
FROM Production.Product [Product]
JOIN Production.ProductProductPhoto PhotoJunction ON
[Product].ProductId = PhotoJunction.ProductId
JOIN Production.ProductPhoto Photo
ON Photo.ProductPhotoId = PhotoJunction.ProductPhotoId
WHERE Name LIKE '%Chain%'
ORDER BY Name
FOR XML RAW('ChainElement'),
ELEMENTS XSINIL,
ROOT('ChainDoc'),
XMLSCHEMA('urn:www-samspublishing-com:examples'),
BINARY BASE64
go
<ChainDoc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsd:schema targetNamespace="urn:www-samspublishing-com:examples"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes"
elementFormDefault="qualified">
<xsd:import
namespace=http://schemas.microsoft.com/sqlserver/2004/sqltypes
schemaLocation="http://schemas.microsoft.com/sqlserver/2004/
sqltypes/sqltypes.xsd"
/>
<xsd:element name="ChainElement">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" nillable="1">
<xsd:simpleType sqltypes:sqlTypeAlias="[AdventureWorks2008].[dbo].[Name]">
<xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033"
sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType
IgnoreWidth" sqltypes:sqlSortId="52">
<xsd:maxLength value="50" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="ListPrice" type="sqltypes:money" nillable="1" />
<xsd:element name="Color" nillable="1">
<xsd:simpleType>
<xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033"
sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType
IgnoreWidth" sqltypes:sqlSortId="52">
<xsd:maxLength value="15" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Weight" nillable="1">
<xsd:simpleType>
<xsd:restriction base="sqltypes:decimal">
<xsd:totalDigits value="8" />
<xsd:fractionDigits value="2" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element
name="ThumbNailPhoto" type="sqltypes:varbinary" nillable="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<ChainElement xmlns="urn:www-samspublishing-com:examples">
<Name>Chain</Name>
<ListPrice>20.2400</ListPrice>
<Color>Silver</Color>
<Weight xsi:nil="true" />
<ThumbNailPhoto>R0lGODlhUAAxAPcAAKeamoyLj {...}</ThumbNailPhoto>
</ChainElement>
</ChainDoc>
Other -----------------
- Programming with SQL Azure : Connecting to SQL Azure (part 4) - Sqlcmd
- Programming with SQL Azure : Connecting to SQL Azure (part 3) - ODBC
- Programming with SQL Azure : Connecting to SQL Azure (part 2)
- Programming with SQL Azure : Connecting to SQL Azure (part 1) - ADO.NET
- Programming with SQL Azure : Application Deployment Factors
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 3)
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 2)
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 1)
- SQL Server 2008: SQL Server Web Services
- SQL Server 2008: SQL Server Service Broker - Related System Catalogs
- SQL Azure Backup Strategies (part 2)
- SQL Azure Backup Strategies (part 1) - Copying a Database
- SQL Server 2008: Troubleshooting SSB Applications with ssbdiagnose.exe
- SQL Server 2008: Service Broker Routing and Security
- Migrating Databases and Data to SQL Azure (part 9)
- Migrating Databases and Data to SQL Azure (part 8)
- Understanding Service Broker Constructs (part 5)
- Understanding Service Broker Constructs (part 4) - Creating the Conversation Initiator
- Migrating Databases and Data to SQL Azure (part 7)
- Migrating Databases and Data to SQL Azure (part 6) - Building a Migration Package
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us